Dot Product

The dot product of two vectors is also called a scalar product. It is the product of the projection of one vector along the axis of the other vector times the length of the other vector. (More on this later.)

Suppose that you have two vectors:

$$\vec{a}=<0.5,2,0>$$$$\vec{b}=<2,0,0>$$

The dot product is:

$$\vec{a}\cdot\vec{b}$$

which can be calculated two ways:

$$\vec{a}\cdot\vec{b}=a_xb_x + a_yb_y+a_zb_z$$

and

$$\vec{a}\cdot\vec{b}=|\vec{a}||\vec{b}|\cos\theta$$

Computing the dot product with iVisual

iVisual has the function $\mathrm{dot}(a,b)$ which takes as its arguments two vectors and returns the dot product. Let's compute the dot product $\vec{a}\cdot\vec{b}$ where $\vec{a}=<0.5,2,0>$ and $\vec{b}=<2,0,0>$. Also compute the angle between the vectors.


In [11]:
from __future__ import division, print_function
from ivisual import *
from math import *

In [12]:
#define the vectors
a=vector(0.5,2,0)
b=vector(2,0,0)

#compute the dot product and print
aDotb=dot(a,b)
print("a dot b = ", aDotb)

#compute the angle between the vectors
#convert to deg
thetaInrad = acos(aDotb/mag(a)/mag(b))
theta = thetaInrad*180/pi
print("the angle between the vectors is ", theta, " deg")


a dot b =  1.0
the angle between the vectors is  75.9637565321  deg

Visualizing the dot product

First, let's draw the vectors in iVisual.


In [13]:
scene=canvas(title="Dot Product of Vectors")

#draw arrows
sw=0.05*mag(a)
aarrow=arrow(pos=(0,0,0), axis=a, color=color.red, shaftwidth=sw)
barrow=arrow(pos=(0,0,0), axis=b, color=color.blue, shaftwidth=sw)


Now let's visualize the dot product. The dot product is the product of:

  1. the component of one vector that lies along the other vector (called the projection of one vector along the other vector)

times

  1. the length of the other vector

If the angle between two vectors is $90^\circ$, then the projection of one vector along the other vector is zero and the dot product is zero. If the angle between two vectors is $0^\circ$ then all of the first vector lies along the second vector and the dot product is the product of their lengths. So the dot product depends on both factors: 1. how much of one vector lies along the other and 2. the length of the other vector.

Let's draw a cylinder that lies along the axis of each vector and represents the dot product of the two vectors. Note that this is a scalar product. Thus a longer cylinder represents a larger dot product. However, the dot product has no direction. Even though we'll draw the cylinder along the axis of a vector, it actually has no direction. That's why we will draw a cylinder instead of an arrow.

Also, it does not matter which vector we consider "the first vector." In other words if we find the projection of $\vec{b}$ onto $\vec{a}$ and multiply by the length of $\vec{a}$ we get the same result as if we find the projection of $\vec{a}$ onto $\vec{b}$ and multiply by the length of $\vec{b}$. This means that the dot product is commutative so $\vec{a}\cdot\vec{b}=\vec{b}\cdot\vec{a}$.


In [14]:
#draw projection of a onto b
projAontoB=norm(b)*aDotb
projAontoBcyl=cylinder(pos=vector(0,0,0), axis=projAontoB, color=color.magenta, radius=sw)

#draw project of b onto a
projBontoA=norm(a)*aDotb
projBontoAcyl=cylinder(pos=vector(0,0,0), axis=projBontoA, color=color.magenta, radius=sw)

Now give $\vec{a}$ and $\vec{b}$ different components and see how it affects the dot product (represented by the length of the cylinder). Try special cases of the vectors being parallel or perpendicular.


In [ ]: